#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

const int N = 5e4 + 10;

struct node
{
	int begin;
	int end;
	int id;

	bool operator <(const node& a)const
	{
		return begin > a.begin;
	}


}a[N];
int t[N];

priority_queue<node>q;

bool cmp(node& x1, node& x2)
{
	return x1.begin < x2.begin;
}

int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].begin >> a[i].end;
		a[i].id = i;
	}

	sort(a + 1, a + 1 + n, cmp);

	int max_num = 1; t[a[1].id] = 1;

	q.push({ a[1].end,1 });
	for (int i = 2; i <= n; i++)
	{
		if (a[i].begin > q.top().begin)
		{
			node e = q.top();
			q.pop();
			t[a[i].id] = e.end;
			q.push({ a[i].end,e.end });
		}
		else
		{
			max_num++;
			t[a[i].id] = max_num;
			q.push({ a[i].end,max_num });
		}
	}

	cout << max_num << endl;
	for (int i = 1; i <= n; i++) cout << t[i] << endl;

	return 0;
}
